/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.business.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPObject;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.business.domain.AdlZhsq;
import me.zhengjie.utils.*;
import lombok.RequiredArgsConstructor;
import me.zhengjie.business.repository.AdlZhsqRepository;
import me.zhengjie.business.service.AdlZhsqService;
import me.zhengjie.business.service.dto.AdlZhsqDto;
import me.zhengjie.business.service.dto.AdlZhsqQueryCriteria;
import me.zhengjie.business.service.mapstruct.AdlZhsqMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.*;
import java.io.IOException;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author xiahongbo
* @date 2022-05-30
**/
@Slf4j
@Service
@RequiredArgsConstructor
public class AdlZhsqServiceImpl implements AdlZhsqService {

    private final AdlZhsqRepository adlZhsqRepository;
    private final AdlZhsqMapper adlZhsqMapper;
    @PersistenceContext
    private EntityManager em;

    @Override
    public Map<String,Object> queryAll(AdlZhsqQueryCriteria criteria, Pageable pageable){
        Page<AdlZhsq> page = adlZhsqRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(adlZhsqMapper::toDto));
    }

    @Override
    public List<AdlZhsqDto> queryAll(AdlZhsqQueryCriteria criteria){
        return adlZhsqMapper.toDto(adlZhsqRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public AdlZhsqDto findById(String bisAdlZhsqId) {
        AdlZhsq adlZhsq = adlZhsqRepository.findById(bisAdlZhsqId).orElseGet(AdlZhsq::new);
        ValidationUtil.isNull(adlZhsq.getBisAdlZhsqId(),"AdlZhsq","bisAdlZhsqId",bisAdlZhsqId);
        return adlZhsqMapper.toDto(adlZhsq);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AdlZhsqDto create(AdlZhsq resources) {
        resources.setBisAdlZhsqId(IdUtil.simpleUUID()); 
        return adlZhsqMapper.toDto(adlZhsqRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(AdlZhsq resources) {
        AdlZhsq adlZhsq = adlZhsqRepository.findById(resources.getBisAdlZhsqId()).orElseGet(AdlZhsq::new);
        ValidationUtil.isNull( adlZhsq.getBisAdlZhsqId(),"AdlZhsq","id",resources.getBisAdlZhsqId());
        adlZhsq.copy(resources);
        adlZhsqRepository.save(adlZhsq);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String bisAdlZhsqId : ids) {
            adlZhsqRepository.deleteById(bisAdlZhsqId);
        }
    }

    @Override
    public void download(List<AdlZhsqDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (AdlZhsqDto adlZhsq : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public Map<String, Object> queryPgAdl(String adlPcId, Long zhid) {
        // 没做完，但是应该暂时不需要了
        String adlPcSql = StringUtils.isEmpty(adlPcId) ? "select * from bis_adl_pc where  bis_adl_pc_zt='adlzt02'"
                : "select * from bis_adl_pc where  bis_adl_pc_id = :pcID";
        Query adlPcQuery = em.createNativeQuery(adlPcSql);
        if(!StringUtils.isEmpty(adlPcId)){
            adlPcQuery.setParameter("pcID", adlPcId);
        }
        List adlPcList = adlPcQuery.getResultList();
        List<Object> allList = new ArrayList();
        Map<String, Object> adlPcMap = new HashMap<String, Object>();
        for (Object adlPcObj : adlPcList) {
            Object[] adlPcArr = (Object[]) adlPcObj;
            //    System.out.println(arr[0]+""+arr[1]);
            String pdID = adlPcArr[0].toString();
            String adlXmSql1 = "select bis_adl_xm_id, bis_adl_xm_name, bis_adl_xm_px from bis_adl_xm r where bis_adl_pc_id = :pcID order by bis_adl_xm_px";

            String adlXmSql = "select r3.bis_adl_xm_id, " +
                    "       r3.bis_adl_xm_name, " +
                    "       r3.bis_adl_xm_px, " +
                    "       r1.bis_adl_xmxx_id, " +
                    "       r4.path, " +
                    "       r4.path2, " +
                    "       r4.path3 " +
                    "  from bis_adl_xm r3 " +
                    "  left join bis_adl_xmxx r1 on r1.bis_adl_xm_id = r3.bis_adl_xm_id " +
                    "  left join bis_adl_zhsq r2 on r1.bis_adl_xmxx_id = r2.bis_adl_xmxx_id " +
                    "  left join tool_local_storage r4 on to_char(r2.bis_adl_zhsq_id) = r4.ywid " +
                    " where 1 = 1 " +
                    "   and r3.bis_adl_pc_id =  :pcID " +
                    "   and bis_zhsq_id = :zhID  " +
                    "   and r2.ywlx = 'RYLX02' " +
                    " order by r3.bis_adl_xm_px ";
            Query adlXmQuery = em.createNativeQuery(adlXmSql);
            adlXmQuery.setParameter("pcID", pdID);
            adlXmQuery.setParameter("zhID", zhid);
            List adlXmList = adlXmQuery.getResultList();
            for(Object adlXmObj : adlXmList) {
                Map<String, Object> adlXmMap = new HashMap<>();
                Object[] adlXmArr = (Object[]) adlXmObj;
                //   System.out.println(adlXmArr[0]+"---"+adlXmArr[1]);
                String xmID = adlXmArr[0].toString();
                String adlXmxxSql1 = "select bis_adl_xmxx_id, bis_adl_xmxx_name, bis_adl_xmxx_fs, bis_adl_xmxx_px from bis_adl_xmxx " +
                        "where bis_adl_xm_id = :xmID order by bis_adl_xmxx_px";
                
                String adlXmxxSql = "select r1.bis_adl_xmxx_id, " +
                        "       r1.bis_adl_xmxx_name, " +
                        "       r1.bis_adl_xmxx_fs, " +
                        "       r1.bis_adl_xmxx_px, " +
                        "       nvl(r2.bis_adl_xmxx_id, '1') bis_adl_xmxx_id2 " +
                        "  from bis_adl_xmxx r1 left join bis_adl_zhsq r2 on r1.bis_adl_xmxx_id = r2.bis_adl_xmxx_id " +
                        "  and r2.bis_zhsq_id = :zhID " +
                        " and r2.ywlx = 'RYLX02' " +
                        " where 1 = 1 " +
                        " and r1.bis_adl_xm_id = :xmID " +
                        " order by r1.bis_adl_xmxx_px";
                Query adlXmxxQuery = em.createNativeQuery(adlXmxxSql);
                adlXmxxQuery.setParameter("zhID", zhid);
                adlXmxxQuery.setParameter("xmID", xmID);
                List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
                List adlXmxxList = adlXmxxQuery.getResultList();
                for(Object adlXmxxObj : adlXmxxList) {
                    Object[] adlXmxxArr = (Object[]) adlXmxxObj;
                    //    System.out.println(adlXmxxArr[0]+"---"+adlXmxxArr[1]);
                    Map<String, Object> adlXmxxMap = new HashMap<String, Object>();
                    adlXmxxMap.put("adlXmxxId", adlXmxxArr[0]);
                    adlXmxxMap.put("adlXmxxName", adlXmxxArr[1]);
                    adlXmxxMap.put("adlXmxxFs", adlXmxxArr[2]);
                    adlXmxxMap.put("adlXmxxPx", adlXmxxArr[3]);
                    list.add(adlXmxxMap);
                    /*
                    adlXmxxMap.put("", adlXmxxArr[2]);
                    adlXmxxMap.put("", adlXmxxArr[3]);*/
                }
                //    System.out.println("---"+list);
                adlXmMap.put("adlXmList", list);
                adlXmMap.put("adlXmId", adlXmArr[0]);
                adlXmMap.put("adlXmName", adlXmArr[1]);
                adlXmMap.put("adlXmPx", adlXmArr[2]);
                adlXmMap.put("adlXmGroup", adlXmArr[3]);
                adlXmMap.put("outFilePath", adlXmArr[4]);
                adlXmMap.put("inFilePath", adlXmArr[5]);
                adlXmMap.put("inFilePath2", adlXmArr[6]);
                //   System.out.println("adlXmMap---"+adlXmMap);
                allList.add(adlXmMap);
                //   System.out.println("allList---"+allList);
            }
            adlPcMap.put("adlPcId", pdID);
            adlPcMap.put("adlData", allList);
        }
        return adlPcMap;
    }

    @Override
    public Map<String, Object> queryZpAdl(String adlPcId, Long zhid) {
        // 没做完，但是应该暂时不需要了
        String adlPcSql = StringUtils.isEmpty(adlPcId) ? "select * from bis_adl_pc where  bis_adl_pc_zt='adlzt02'"
                : "select * from bis_adl_pc where  bis_adl_pc_id = :pcID";
        Query adlPcQuery = em.createNativeQuery(adlPcSql);
        if(!StringUtils.isEmpty(adlPcId)){
            adlPcQuery.setParameter("pcID", adlPcId);
        }
        List adlPcList = adlPcQuery.getResultList();
        List<Object> allList = new ArrayList();
        Map<String, Object> adlPcMap = new HashMap<String, Object>();
        for (Object adlPcObj : adlPcList) {
            Object[] adlPcArr = (Object[]) adlPcObj;
            //    System.out.println(arr[0]+""+arr[1]);
            String pdID = adlPcArr[0].toString();
            String adlXmSql1 = "select bis_adl_xm_id, bis_adl_xm_name, bis_adl_xm_px from bis_adl_xm r where bis_adl_pc_id = :pcID order by bis_adl_xm_px";

            String adlXmSql = "select r3.bis_adl_xm_id, " +
                    "       r3.bis_adl_xm_name, " +
                    "       r3.bis_adl_xm_px, " +
                    "       r1.bis_adl_xmxx_id, " +
                    "       r4.path " +
                    "  from bis_adl_xm r3 " +
                    "  left join bis_adl_xmxx r1 on r1.bis_adl_xm_id = r3.bis_adl_xm_id " +
                    "  left join bis_adl_zhsq r2 on r1.bis_adl_xmxx_id = r2.bis_adl_xmxx_id " +
                    "  left join tool_local_storage r4 on r2.bis_adl_zhsq_id = r4.ywid " +
                    " where 1 = 1 " +
                    "   and r3.bis_adl_pc_id =  :pcID " +
                    "   and bis_zhsq_id = :zhID  " +
                    "   and r2.ywlx is null " +
                    " order by r3.bis_adl_xm_px ";
            Query adlXmQuery = em.createNativeQuery(adlXmSql);
            adlXmQuery.setParameter("pcID", pdID);
            adlXmQuery.setParameter("zhID", zhid);
            List adlXmList = adlXmQuery.getResultList();
            for(Object adlXmObj : adlXmList) {
                Map<String, Object> adlXmMap = new HashMap<>();
                Object[] adlXmArr = (Object[]) adlXmObj;
                //   System.out.println(adlXmArr[0]+"---"+adlXmArr[1]);
                String xmID = adlXmArr[0].toString();
                String adlXmxxSql1 = "select bis_adl_xmxx_id, bis_adl_xmxx_name, bis_adl_xmxx_fs, bis_adl_xmxx_px from bis_adl_xmxx " +
                        "where bis_adl_xm_id = :xmID order by bis_adl_xmxx_px";

                String adlXmxxSql = "select r1.bis_adl_xmxx_id, " +
                        "       r1.bis_adl_xmxx_name, " +
                        "       r1.bis_adl_xmxx_fs, " +
                        "       r1.bis_adl_xmxx_px, " +
                        "       nvl(r2.bis_adl_xmxx_id, '1') bis_adl_xmxx_id2 " +
                        "  from bis_adl_xmxx r1 left join bis_adl_zhsq r2 on r1.bis_adl_xmxx_id = r2.bis_adl_xmxx_id " +
                        "  and r2.bis_zhsq_id = :zhID " +
                        " and r2.ywlx  is null " +
                        " where 1 = 1 " +
                        " and r1.bis_adl_xm_id = :xmID " +
                        " order by r1.bis_adl_xmxx_px";
                Query adlXmxxQuery = em.createNativeQuery(adlXmxxSql);
                adlXmxxQuery.setParameter("zhID", zhid);
                adlXmxxQuery.setParameter("xmID", xmID);
                List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
                List adlXmxxList = adlXmxxQuery.getResultList();
                for(Object adlXmxxObj : adlXmxxList) {
                    Object[] adlXmxxArr = (Object[]) adlXmxxObj;
                    //    System.out.println(adlXmxxArr[0]+"---"+adlXmxxArr[1]);
                    Map<String, Object> adlXmxxMap = new HashMap<String, Object>();
                    adlXmxxMap.put("adlXmxxId", adlXmxxArr[0]);
                    adlXmxxMap.put("adlXmxxName", adlXmxxArr[1]);
                    adlXmxxMap.put("adlXmxxFs", adlXmxxArr[2]);
                    adlXmxxMap.put("adlXmxxPx", adlXmxxArr[3]);
                    list.add(adlXmxxMap);
                    /*
                    adlXmxxMap.put("", adlXmxxArr[2]);
                    adlXmxxMap.put("", adlXmxxArr[3]);*/
                }
                //    System.out.println("---"+list);
                adlXmMap.put("adlXmList", list);
                adlXmMap.put("adlXmId", adlXmArr[0]);
                adlXmMap.put("adlXmName", adlXmArr[1]);
                adlXmMap.put("adlXmPx", adlXmArr[2]);
                adlXmMap.put("adlXmGroup", adlXmArr[3]);
                adlXmMap.put("filePath", adlXmArr[4]);
                //   System.out.println("adlXmMap---"+adlXmMap);
                allList.add(adlXmMap);
                //   System.out.println("allList---"+allList);
            }
            adlPcMap.put("adlPcId", pdID);
            adlPcMap.put("adlData", allList);
        }
        return adlPcMap;
    }

    @Override
    public Map<String, Object> queryOrgAdl(String adlPcId, Long zhid) {
        // 没做完，但是应该暂时不需要了
        String adlPcSql = StringUtils.isEmpty(adlPcId) ? "select * from bis_adl_pc where  bis_adl_pc_zt='adlzt02'"
                : "select * from bis_adl_pc where  bis_adl_pc_id = :pcID";
        Query adlPcQuery = em.createNativeQuery(adlPcSql);
        if(!StringUtils.isEmpty(adlPcId)){
            adlPcQuery.setParameter("pcID", adlPcId);
        }
        List adlPcList = adlPcQuery.getResultList();
        List<Object> allList = new ArrayList();
        Map<String, Object> adlPcMap = new HashMap<String, Object>();
        for (Object adlPcObj : adlPcList) {
            Object[] adlPcArr = (Object[]) adlPcObj;
            String pdID = adlPcArr[0].toString();

            String adlXmSql = "select r3.bis_adl_xm_id, " +
                    "       r3.bis_adl_xm_name, " +
                    "       r3.bis_adl_xm_px, " +
                    "       r1.bis_adl_xmxx_id, " +
                    "       r4.path2, " +
                    "       r4.storage_id, " +
                    "       r4.name " +
                    "  from bis_adl_xm r3 " +
                    "  left join bis_adl_xmxx r1 on r1.bis_adl_xm_id = r3.bis_adl_xm_id " +
                    "  left join bis_adl_zhsq r2 on r1.bis_adl_xmxx_id = r2.bis_adl_xmxx_id " +
                    "  left join tool_local_storage r4 on r2.bis_adl_zhsq_id = r4.ywid " +
                    " where 1 = 1 " +
                    "   and r3.bis_adl_pc_id =  :pcID " +
                    "   and bis_zhsq_id = :zhID  " +
                    "   and r2.ywlx = 'RYLX99' " +
                    "   and r2.BIS_ZFBS is null " +
                    " order by r3.bis_adl_xm_px ";
            Query adlXmQuery = em.createNativeQuery(adlXmSql);
            adlXmQuery.setParameter("pcID", pdID);
            adlXmQuery.setParameter("zhID", zhid);
            List adlXmList = adlXmQuery.getResultList();
            for(Object adlXmObj : adlXmList) {
                Map<String, Object> adlXmMap = new HashMap<>();
                Object[] adlXmArr = (Object[]) adlXmObj;
                String xmID = adlXmArr[0].toString();

                String adlXmxxSql = "select r1.bis_adl_xmxx_id, " +
                        "       r1.bis_adl_xmxx_name, " +
                        "       r1.bis_adl_xmxx_fs, " +
                        "       r1.bis_adl_xmxx_px, " +
                        "       nvl(r2.bis_adl_xmxx_id, '1') bis_adl_xmxx_id2 " +
                        "  from bis_adl_xmxx r1 left join bis_adl_zhsq r2 on r1.bis_adl_xmxx_id = r2.bis_adl_xmxx_id " +
                        "  and r2.bis_zhsq_id = :zhID " +
                        "   and r2.ywlx = 'RYLX99' " +
                        "   and r2.BIS_ZFBS is null " +
                        " where 1 = 1 " +
                        " and r1.bis_adl_xm_id = :xmID " +
                        " order by r1.bis_adl_xmxx_px";
                Query adlXmxxQuery = em.createNativeQuery(adlXmxxSql);
                adlXmxxQuery.setParameter("zhID", zhid);
                adlXmxxQuery.setParameter("xmID", xmID);
                List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
                List adlXmxxList = adlXmxxQuery.getResultList();
                for(Object adlXmxxObj : adlXmxxList) {
                    Object[] adlXmxxArr = (Object[]) adlXmxxObj;
                    //    System.out.println(adlXmxxArr[0]+"---"+adlXmxxArr[1]);
                    Map<String, Object> adlXmxxMap = new HashMap<String, Object>();
                    adlXmxxMap.put("adlXmxxId", adlXmxxArr[0]);
                    adlXmxxMap.put("adlXmxxName", adlXmxxArr[1]);
                    adlXmxxMap.put("adlXmxxFs", adlXmxxArr[2]);
                    adlXmxxMap.put("adlXmxxPx", adlXmxxArr[3]);
                    list.add(adlXmxxMap);
                    /*
                    adlXmxxMap.put("", adlXmxxArr[2]);
                    adlXmxxMap.put("", adlXmxxArr[3]);*/
                }
                //    System.out.println("---"+list);
                adlXmMap.put("adlXmList", list);
                adlXmMap.put("adlXmId", adlXmArr[0]);
                adlXmMap.put("adlXmName", adlXmArr[1]);
                adlXmMap.put("adlXmPx", adlXmArr[2]);
                adlXmMap.put("adlXmGroup", adlXmArr[3]);
                adlXmMap.put("inFilePath2", adlXmArr[4]);
                adlXmMap.put("fileId", adlXmArr[5]);
                adlXmMap.put("fileName", adlXmArr[6]);
                //   System.out.println("adlXmMap---"+adlXmMap);
                allList.add(adlXmMap);
                //   System.out.println("allList---"+allList);
            }
            adlPcMap.put("adlPcId", pdID);
            adlPcMap.put("adlData", allList);
        }
        return adlPcMap;
    }
}